
package com.catsic.zcjc.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import com.catsic.base.BaseServiceImpl;
import com.catsic.base.exception.CatsicException;
import com.catsic.base.tree.TreeInfo;
import com.catsic.core.conditon.OrganCondition;
import com.catsic.core.convert.Converts;
import com.catsic.core.entity.Organ;
import com.catsic.core.exception.CatsicExceptionCode;
import com.catsic.core.organ.dao.OrganDao;
import com.catsic.core.organ.service.OrganService;
import com.catsic.core.specification.OrganSpec;
import com.catsic.pub.drawer.service.DrawerService;
import com.catsic.pub.entity.Drawer;
import com.catsic.zcjc.condition.StationCondition;
import com.catsic.zcjc.constants.ZcjcConstants;
import com.catsic.zcjc.convert.ZcjcConverts;
import com.catsic.zcjc.dao.StationDao;
import com.catsic.zcjc.dao.jdbc.JdbcStationDao;
import com.catsic.zcjc.entity.District;
import com.catsic.zcjc.entity.Station;
import com.catsic.zcjc.enums.AreaType;
import com.catsic.zcjc.exception.ZcjcExceptionCode;
import com.catsic.zcjc.form.StationCountForm;
import com.catsic.zcjc.form.StationForm;
import com.catsic.zcjc.service.DistrictService;
import com.catsic.zcjc.service.StationService;
import com.catsic.zcjc.specifiction.StationSpec;


@Service("stationService")
public class StationServiceImpl extends BaseServiceImpl<Station, String> implements StationService{
	
	/**
	 * StationDao:站点Dao
	 */
	@Autowired
	private StationDao stationDao;
	
	@Autowired
	private JdbcStationDao jdbcStationDao;
	
	@Autowired
	private OrganDao organDao;

	@Autowired
	private OrganService organService;
	
	@Autowired
	private DistrictService districtService;
	
	@Autowired
	private DrawerService drawerService;

	@Override
	public StationForm add(StationForm form) {
		checkStationSame(form);
		
		Organ organ = organService.findById(form.getOrganId());
		Station station = new Station();
		
		District district = districtService.findById(form.getDistrictId());
		
		station.setAddress(form.getAddress());
		
		station.setCode(form.getCode());
		station.setLatitude(form.getLatitude());
		station.setLongitude(form.getLongitude());
		station.setName(form.getName());
		station.setOrgan(organ);
		station.setRemark(form.getRemark());
		station.setType(form.getType());
		station.setCreateId(form.getCreateId());
		station.setCreateName(form.getCreateName());
		station.setCityCode(district);
		station.setTypeSon(form.getTypeSon());
		station.setCreateId(form.getCreateId());
		station.setCreateName(form.getCreateName());
		
		return ZcjcConverts.convert(super.add(station));
	}
	

	@Override
	public StationForm update(StationForm form) {
		checkStationSame(form);
		
		Station station = findById(form.getId());
		
		District district = districtService.findById(form.getDistrictId());
		
		station.setAddress(form.getAddress());
		station.setCityCode(district);
		station.setCode(form.getCode());
		station.setLatitude(form.getLatitude());
		station.setLongitude(form.getLongitude());
		station.setName(form.getName());
		station.setRemark(form.getRemark());
		station.setType(form.getType());
		station.setTypeSon(form.getTypeSon());
		
		return ZcjcConverts.convert(super.add(station));
	}
	

	@Override
	public Station findById(String id){
		Station Station = getById(id);
		if(null == Station){
			throw new CatsicException(CatsicExceptionCode.实体不存在, ZcjcConstants.STATION);
		}
		return Station;
	}
	

	@Override
	public StationForm findStationById(String id){
		Station station = findById(id);
		StationForm form = ZcjcConverts.convert(station);
		
		
		return form;
	}
	

	
	public void checkStationSame(StationForm form) {
		if(hasSameStation(form)){
			throw new CatsicException(ZcjcExceptionCode.站点管理_编码重复);
		}
	}

	public boolean hasSameStation(StationForm form) {
		Station station = stationDao.findByCode(form.getCode());
		if(form.getId()==null){
			if(station!=null){
				return true;
			}
		}else{
			if(station!=null&&!station.getId().equals(form.getId())){
				return true;
			}
		}
		
		return false;
	}
	
	public Page<StationForm> findAll(StationCondition condition, Pageable pageable){
		StationSpec spec = new StationSpec(condition);
		Page<Station> page = stationDao.findAll(spec, pageable);
		List<StationForm> listForm = new ArrayList<StationForm>();
		for(Station station :page.getContent()){
			listForm.add(ZcjcConverts.convert(station));
	    }
	    Page<StationForm> page_ = new PageImpl<StationForm>(listForm,pageable,page.getTotalElements());
	    return page_;
	}


	@Override
	public List<StationForm> query(String organId) {
		List<Station> list = this.findByOrganId(organId);
		
		List<StationForm> listForm = new ArrayList<StationForm>();
		
		if(CollectionUtils.isNotEmpty(listForm)){
			for(Station station :list){
				listForm.add(ZcjcConverts.convert(station));
		    }
		}
		
		return listForm;
	}


	@Override
	public List<Station> findByOrganId(String organId) {
		return stationDao.findByOid(organId);
	}
	
	@Override
	public void delete(String id){
		Station station = findById(id);
		
		if(!CollectionUtils.isEmpty(station.getSystems())){
			throw new CatsicException(ZcjcExceptionCode.站点管理_站点下有系统_无法删除);
		}

		super.delete(station);
	}
	
	public List<TreeInfo> getTreeInfo(String id){
		List<Organ> list = organService.findByPid(id);
		List<TreeInfo> listForm = new ArrayList<TreeInfo>();
		
		if(!CollectionUtils.isEmpty(list)){
			for(Organ organ : list){
				TreeInfo treeInfo = Converts.convertTree(organ);
				listForm.add(treeInfo);
			}
		}else{
			//插叙机构下面是否有站点
			List<Station> stationList = this.findByOrganId(id);
			if(!CollectionUtils.isEmpty(stationList)){
				for(Station station : stationList){
					TreeInfo treeInfo = ZcjcConverts.convertTree(station);
					listForm.add(treeInfo);
				}
			}
		}
		return listForm;
	}
	
	public List<TreeInfo> query(OrganCondition condition){
		OrganSpec spec = new OrganSpec(condition);
		List<Organ> list = organDao.findAll(spec);
		List<TreeInfo> listForm = new ArrayList<TreeInfo>();
		for(Organ organ : list){
			TreeInfo treeInfo = Converts.convertTree(organ);
			if(!listForm.contains(treeInfo))
				listForm.add(treeInfo);
			Organ parent = organ;
			while(parent != null && !StringUtils.equals(parent.getCode(), condition.getCode())){
				treeInfo = Converts.convertTree(parent);
				if(!listForm.contains(treeInfo))
					listForm.add(treeInfo);
				parent = parent.getParent();
			}
		}
		return listForm;
	}


	@Override
	public List<StationCountForm> findCountByArea(String type,Integer areaId) {
		List<StationCountForm> list = new ArrayList<StationCountForm>();
		
		List<Map<String,Object>> dataList = jdbcStationDao.getByArea(type,areaId,null);
		if(CollectionUtils.isNotEmpty(dataList)){
			for(Map<String,Object> map : dataList){
				list.add(ZcjcConverts.convert(map));
			}
		}
		
		return list;
	}
	
	
	
	@Override
	public List<StationCountForm> findCountByAreaAndOrg(Organ organ,Integer areaId){
		List<StationCountForm> list = new ArrayList<StationCountForm>();
		
		String type = null;
		if(organ.getJb()==0){
			//部
			type = AreaType.province.toString();
		}else if(organ.getJb()==1){
			//部
			type = AreaType.city.toString();
		}else if(organ.getJb()>=2){
			//部
			type = AreaType.district.toString();
		}
		
		if(StringUtils.isNotBlank(type)){
			List<Map<String,Object>> dataList = jdbcStationDao.getByArea(type,areaId,organ.getCode());
			StationCountForm total = new StationCountForm();
			total.setAreaName("合计");
			list.add(total);
			
			if(CollectionUtils.isNotEmpty(dataList)){
				for(Map<String,Object> map : dataList){
					StationCountForm form = ZcjcConverts.convert(map);
					list.add(form);
					
					total.setTotalCount(total.getTotalCount()+form.getTotalCount());
					total.setTypeCount1(total.getTypeCount1()+form.getTypeCount1());
					total.setTypeCount2(total.getTypeCount2()+form.getTypeCount2());
					total.setTypeCount3(total.getTypeCount3()+form.getTypeCount3());
					total.setTypeCount4(total.getTypeCount4()+form.getTypeCount4());
					total.setTypeCount5(total.getTypeCount5()+form.getTypeCount5());
					
				}
			}
			
			
		}
		return list;
	}
	
	
	public void findFormatDrawer(StationForm form){
		Drawer typeD = null;
		if(form.getType()!=null){
			typeD = drawerService.findByParentAndValue(ZcjcConstants.DRAWER_STATION_TYPE, form.getType().toString());
			if(typeD!=null){
				form.setCnType(typeD.getName());
			}
		}
		
		if(typeD!=null&&StringUtils.isNotBlank(form.getTypeSon())){
			Drawer d = drawerService.findByParentAndValue(typeD.getCode(), form.getTypeSon());
			if(d!=null){
				form.setCnTypeSon(d.getName());
			}
		}
	}



}
